#pragma once

#include <iostream>
#include <sys/epoll.h> // epoll 函数和常量
#include <fcntl.h>     // 用于 fcntl()，设置非阻塞模式
#include <unistd.h>
#include "log.hpp"

using namespace std;

class Epoll
{
public:
    Epoll()
    {
        epfd_ = epoll_create(1);
        if (epfd_ < 0)
        {
            lg("Error", "epfd create error");
            exit(1);
        }
        else
        {
            lg("logo","epfd create success");
        }
    }

    int Epoll_Wait(struct epoll_event* events,int num)
    {
        int n=epoll_wait(epfd_,events,num,-1);
        return n;
    }

    int Epoll_Updata(int op, int fd, uint32_t event)//event是执行的读写，操作等
    {
        int n = 0;
        if (op == EPOLL_CTL_DEL)
        {
            n = epoll_ctl(epfd_, op, fd, nullptr);
            if (n < 0)
            {
                //lg("Error","epoll_ctl del error");
                //   perror("error: ");
                return -1;
            }
        }
        else
        {
            //EPOLL_CTL_ADD EPOLL_CTL_MOD
            struct epoll_event ev;
            ev.data.fd=fd;
            ev.events=event;

            n=epoll_ctl(epfd_,op,fd,&ev);
            if(n<0)
            {
                lg("Error","epoll_ctl mod/add error");
                return -1;
            }
        }
        return n;
    }

    ~Epoll()
    {
        close(epfd_);
    }

private:
    int epfd_;
    int timeout= {2000};
};